package org.example.pmsystem.controller;

import org.example.pmsystem.Response;
import org.example.pmsystem.entity.StaffLeave;
import org.example.pmsystem.service.StaffLeaveService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@CrossOrigin(origins = "http://localhost:5173")
@RequestMapping("/staffLeave")
public class StaffLeaveController {
    @Autowired
    private StaffLeaveService staffLeaveService;
    @GetMapping("getStaffLeaveBySno/{sNo}")
    public Response<List<StaffLeave>> getStaffLeaveBySno(@PathVariable String sNo){
        return Response.newSuccess(staffLeaveService.getStaffLeaveBySno(sNo));
    }

    @GetMapping("getStaffLeaveByDep/{sDep}")
    public Response<List<StaffLeave>> getAllStaffLeaveByDep(@PathVariable String sDep){
        System.out.println(sDep);
        return Response.newSuccess(staffLeaveService.getStaffLeaveByDep(sDep));
    }

    @GetMapping("getStaffLeaveByStatus/{status}")
    public Response<List<StaffLeave>> getAllStaffLeaveByStatus(@PathVariable String status){
        return Response.newSuccess(staffLeaveService.getStaffLeaveByStatus(status));
    }

    @PostMapping("insertStaffLeave")
    public Response<Boolean> insertStaffLeave(@RequestBody StaffLeave staffLeave) {
        if (staffLeaveService.insertStaffLeave(staffLeave))
            return Response.newSuccess(true);
        return Response.newFail("请假日期有误！");
    }

    @PutMapping("updateStaffLeave")
    public Response<Boolean> updateStaffLeave(@RequestBody StaffLeave staffLeave){
        if (staffLeaveService.updateStaffLeave(staffLeave))
            return Response.newSuccess(null);
        return Response.newFail("请假日期有误！");
    }

    @PutMapping("updateStaffLeaveStatus")
    public Response<Void> updateStaffLeaveStatus(@RequestBody StaffLeave staffLeave) {
        staffLeaveService.updateStaffLeaveStatus(staffLeave);
        return Response.newSuccess(null);
    }

}
